package yxt.net.websocket.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.*;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
import yxt.net.websocket.example.handler.TextWebSocketAction;
import yxt.net.websocket.example.handler.TextWebSocketAction2;

import javax.annotation.Resource;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author zhangxiaolin
 * @since 2022/1/25$ 13:32$
 */
@EnableWebSocket
//@EnableWebSocketMessageBroker  同 createEndpoint
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
    @Resource
    private TextWebSocketAction textWebSocketAction;
    @Resource
    private TextWebSocketAction2 textWebSocketAction2;
//    @Resource
//    private HttpSessionHandshakeInterceptor chatHandshakeInterceptor;

//    @Bean
//    public ServletServerContainerFactoryBean createWebSocketContainer() {
//        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
//        container.setMaxTextMessageBufferSize(8192);
//        container.setMaxBinaryMessageBufferSize(8192);
//        return container;
//    }
//    @Bean
//    WebSocketConfigurer createWebSocketConfigurer(
//            @Autowired WebSocketHandler chatHandler,
//            @Autowired HandshakeInterceptor chatInterceptor)
//    {
//        return new WebSocketConfigurer() {
//            @Override
//            public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//                // 把URL与指定的WebSocketHandler关联，可关联多个:
//                registry.addHandler(chatHandler, "/chat").addInterceptors(chatInterceptor);
//            }
//        };
//    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(textWebSocketAction, "/textHandler").addHandler(textWebSocketAction2,"/textHandler2").setAllowedOrigins("*").addInterceptors(new HttpSessionHandshakeInterceptor());//.withSockJS();

    }
    //第二种方案
//    @Bean
//    public WebSocketMessageBrokerConfigurer createEndpoint(){
//        return new WebSocketMessageBrokerConfigurer(){
//            @Override
//            public void registerStompEndpoints(StompEndpointRegistry registry) {
//                registry.addEndpoint("/wsnet").setAllowedOrigins("*").withSockJS();
//            }
//        };
//    }
}
